<template>
  <div>
    <!-- 搜索区域 -->
    <div class="search-bar">
        <div style="display: flex; justify-content: flex-start;">
          <el-form :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item label="搜索：">
            <el-select v-model="searchForm.condition" placeholder="请选择" style="width: 120px;">
                <el-option label="全部" value="all"></el-option>
                <el-option label="订单标题" value="orderTitle"></el-option>
                <el-option label="游戏类型" value="gameTypeName"></el-option>
            </el-select>
            </el-form-item>

            <el-form-item>
            <el-input v-model="searchForm.keyword" placeholder="请输入搜索内容" style="width: 160px;"></el-input>
            </el-form-item>

            <el-form-item label="状态搜索：">
            <el-select v-model="searchForm.statusCondition" placeholder="请选择" style="width: 120px;">
                <el-option label="全部" value=""></el-option>
                <el-option label="已发布" value="0"></el-option>
                <el-option label="审核中" value="2"></el-option>
                <el-option label="预定" value="7"></el-option>
                <el-option label="游戏中" value="1"></el-option>
                <el-option label="已完成" value="3"></el-option>
            </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleSearch" style="height: 32px; padding: 0 12px; font-size: 12px;">搜索</el-button>
            </el-form-item>

            <el-form-item>
              <el-button @click="resetSearch" style="height: 32px; padding: 0 12px; font-size: 12px;">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
    </div>

    <!-- 订单列表 -->
    <el-table :data="orderList" height="510" border style="width: 100%">
        <el-table-column 
            :label="header" 
            v-for="header  in tableHeader" :key="header" 
            :prop="getPropName(header)"
            width="160" show-overflow-tooltip>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
        <el-pagination
            @size-change="changePageSize"               
            @current-change="handleCurrentChange"
            :current-page="params.pageNum"
            :page-sizes="[2, 3, 4, 5,7,9]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="count">
        </el-pagination>
    </div>    
  </div>
</template>

<script>
export default {
  data(){
    return{
      tableHeader:[
        '订单ID', '订单标题', '陪玩', '老板','订单量/时间','订单单价','订单状态',
        '游戏类型', '开始时间','结束时间','实际时长','实际局数',
        '订单金额','打赏','评分','创建时间'
      ], 
      orderList:[],
      count:0,        //总条数
      params:{
          pageNum:1,      //当前页
          pageSize:9,     //页容量
      },
      searchForm: {
        condition: '', // 搜索条件字段名
        statusCondition: '',  // 状态搜索条件字段名
        keyword: ''    // 搜索关键词
      },
    }
  },
  methods:{
    // 根据表头获取对应的属性名
    getPropName(header){
      const mapping = {
        '订单ID': 'id',
        '订单标题': 'orderTitle',
        '陪玩': 'playmateNickname',
        '老板': 'bossNickname',
        '订单量/时间': 'orderUnit',
        '订单单价': 'orderPrice',
        '订单状态': 'orderStatus',
        '游戏类型': 'gameTypeName',
        '开始时间': 'orderStartTime',
        '结束时间': 'orderEndTime',
        '实际时长': 'orderActualDuration',
        '实际局数': 'orderActualNumber',
        '订单金额': 'orderMoney',
        '打赏': 'orderTip',
        '评分': 'orderRating',
        '创建时间': 'createTime'
      }
      return mapping[header];
    },
    // 加载订单列表
    async loadOrderList(){
      const requestData = {
          pageNum: this.params.pageNum,
          pageSize: this.params.pageSize,
          tenantId: 1, // 需要根据实际情况设置tenantId
          searchOrderTitle: this.searchForm.condition === 'orderTitle' ? this.searchForm.keyword : null,
          searchPlaymateId: this.searchForm.condition === 'playmateId' ? this.searchForm.keyword : null,
          searchBossId: this.searchForm.condition === 'bossId' ? this.searchForm.keyword : null,
          searchGameTypeName: this.searchForm.condition === 'gameTypeName' ? this.searchForm.keyword : null,
          searchOrderStatus: this.searchForm.statusCondition || null,
        };
        const resp = await this.customerServiceApi.queryAllOrderInfo(requestData);
        if (resp.code == 200) { 
          this.orderList = resp.data.list.map(item => {
            // 处理订单状态显示
            const statusMap = {
              0: '已发布',
              1:'游戏中',
              7: '预定',
              2: '审核中',
              3: '已完成'
            };
            return {
              ...item,
              orderStatus: statusMap[item.orderStatus] 
            };
          });
          this.count = resp.data.total;
        }else{
          this.$message.error(resp.message);
        }
    },
    //搜索
    handleSearch() {
      this.params.pageNum = 1; // 搜索时重置到第一页
      this.loadOrderList();
    },
    // 重置搜索条件
    resetSearch() {
      this.searchForm.condition = '';
      this.searchForm.statusCondition = '';
      this.searchForm.keyword = '';
      this.params.pageNum = 1;
      this.loadOrderList(); // 重新加载列表
    },
     // 改变页大小
    changePageSize(val) {
      this.params.pageSize = val;
      this.params.pageNum = 1; // 重置到第一页
      this.loadOrderList();
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.loadOrderList();
    }
  },
  created(){
    this.loadOrderList();
  }
};
</script>

<style scoped>
.search-bar {
  padding: 5px 0; /* 减小上下内边距 */
}

.demo-form-inline {
  display: flex;
  align-items: center;
  gap: 8px; /* 减小组件之间的间距 */
}

.el-form-item {
  margin-top: 0; /* 移除默认的顶部外边距 */
  margin-bottom: 0; /* 移除默认的底部外边距 */
}

.el-select .el-input__inner {
  height: 30px; /* 进一步减小高度 */
  font-size: 12px;
}

.el-input .el-input__inner {
  height: 30px; /* 进一步减小高度 */
  font-size: 12px;
}

.el-button {
  height: 30px; /* 进一步减小高度 */
  padding: 0 10px; /* 减小内边距 */
  font-size: 12px;
}
</style>
